package movie.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Film;
import movie.service.FilmDeleteService;
import movie.service.FilmFindAllService;
import movie.service.FilmInsertService;


@Controller
public class FilmDeleteController {
	
	@Autowired
	private FilmDeleteService filmdeleteService;
	@Autowired
	private FilmFindAllService filmFindAllService;

	
	@RequestMapping(value = "/filmDelete", method = RequestMethod.GET)
	public String get( Model model) {
		List<Film> films = filmFindAllService.findAll();
		for(Film film:films)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			film.setReleaseDateString(format1.format(film.getReleaseDate()));				
		}
		model.addAttribute("films", films);
		return "Film/deletefilm";
	}
	
	@RequestMapping(value = "/filmDelete", method = RequestMethod.POST)
	public String post(int movieId,Model model) {
		
		int rows = filmdeleteService.deleteFilm(movieId);
		
		List<Film> films = filmFindAllService.findAll();
		model.addAttribute("films", films);
		//forward
		return "redirect:/filmFindAll";
	}
	
	@RequestMapping(value = "/filmfind/filmname/delete", method = RequestMethod.GET)
	public String getfilmname(String filmName,Model model) {
		if(!filmName.equals(""))
		{
			List<Film> films = filmFindAllService.findByFilmName(filmName);
			for(Film film:films)
			{
				
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				film.setReleaseDateString(format1.format(film.getReleaseDate()));	

			}
			model.addAttribute("films", films);
			return "Film/deletefilm";
		}
		else 
		{
			List<Film> films = filmFindAllService.findAll();
			for(Film film:films)
			{
				
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				film.setReleaseDateString(format1.format(film.getReleaseDate()));	

			}
			model.addAttribute("films", films);
			return "Film/deletefilm";
		}
				
	}
	
	
	
}
